﻿@{
    ViewBag.Title = "Home";
}
@model Auction_Intranet.Models.DashBoardInfoModel
@using Auction_Intranet.Helpers;
 
<section class="dashboard page">
    <h2 class="page-title">Dashboard</h2>           

    <aside id="tabs-left" class="tabs-dashboard">
        @{ int ctn = 0; int ctnColor = -1;}
	    <ul class="tabs-left-list">
            @foreach (var item in Model.rooms)
            {
                ctn++; ctnColor++;
                <li data-id="@ctnColor"><a href="#tabs-bids-@ctn">@item.Name</a></li>
            }       
	    </ul>
        @{ int ctn2 = 1;}
        @foreach (var item in Model.rooms)
        {
	        <div id="tabs-bids-@ctn2">                
                <p class="tabs-title">Last auction result: @Model.currentDateResult.ToLongDateString()<span class="calendar-icon"></span></p>
                <table class="tabs-bids-result" cellspacing="0">
                    <tbody>
                    @{ string bidItemClass = "bidentry"; string groupName = ""; }
                    @foreach (var item2 in Model.bidTimes)
                    {
                        var tempResult = Model.DayResults.Where(x => x.RoomId == item.Id && x.StartTime == item2.Startime).FirstOrDefault();                                           
                        <tr>
		                    <td class="col-1 time">@item2.Startime.ToShortTimeString() - @item2.Endtime.ToShortTimeString()</td>                            
                            @if (tempResult != null)
                            {
                                int start = Model.bidTimes.Where(x => x.Startime == (tempResult.StartTime)).Select(x => x.Id).FirstOrDefault();
                                int end = Model.bidTimes.Where(x => x.Endtime == (tempResult.EndTime)).Select(x => x.Id).FirstOrDefault();
                                bidItemClass = "bidentry be-size-" + (end - start + 1).ToString();
                                groupName = tempResult.Group.Name;
                            }
                            else
                            {
                                bidItemClass = "";
                                groupName = "";
                            }  
		                    <td class="col-2 group-name @bidItemClass">
                                <div class="col-2-innerwrapper">
                                    <p>@groupName</p>
                                </div>
                            </td>                            
                        </tr>
                    }
                    @{ ctn2++; }
                    </tbody>
                </table>
                <p class="tabs-date-paging">
                    @{ long prevTicks = Model.currentDateResult.AddDays(-1).Ticks; }
                    <!--<span class="prev">@Html.ActionLink(" ", "Index", new { ticks = prevTicks })</span>-->                    
                    <span class="prev"><span class="loading-icon"></span><a href="#" title="Previous day"></a></span>
                    <span class="current">@Model.currentDateResult.ToLongDateString()</span>
                    @{ var currentDate = DateTime.Now.Date; var displayDate = Model.currentDateResult.Date; }
                    @if (displayDate < currentDate)
                    {
@*                        long nextTicks = Model.currentDateResult.AddDays(1).Ticks;
                        <span class="next">@Html.ActionLink(" ", "Index", new { ticks = nextTicks })</span>*@
                        <span class="next"><a href="#" title="Next day"></a></span>
                    }                               
                </p> 
	        </div>       
        }
    </aside>      

    <aside class="top-bids-dashboard">
        <section class="future-bids">
            <h2>Future Bids<span class="bids-icon"></h2>
            <table class="bids-history" cellspacing="0">
                <tbody>
                    @foreach (var item in Model.PendingBids)
                    { 
                        <tr>
                            <td>@item.Date.Day.ToString().@item.Date.Month.ToString().@item.Date.Year.ToString()</td>
                            <td>@FileHelper.Truncate(item.Group.Name,10)</td>
                            @if (item.AnyRoom == true)
                            {
                                <td>Any</td>
                            }
                            else 
                            { 
                                <td>@item.Room.Name</td> 
                            } 
                            <td>@item.BiddingAmount</td>
                        </tr>
                    }                   
                </tbody>
            </table>
        </section>
        <section class="past-bids">
            <h2>Past Bids<span class="bids-icon"></h2>
            <table class="bids-history" cellspacing="0">
                <tbody>
                @foreach (var item in Model.PastBids)
                { 
                    <tr>
                        <td>@item.Date.Day.ToString().@item.Date.Month.ToString().@item.Date.Year.ToString()</td>
                        <td>@FileHelper.Truncate(item.Group.Name,10)</td>                         
                        @if (item.StateId == 3)
                        {
                            if (item.AnyRoom == true)
                            {
                                <td>Any</td>
                            }
                            else
                            { 
                                <td>@item.Room.Name</td> 
                            }
                        }
                        else
                        {
                            <td>@item.Room.Name</td>
                        }                                       
                        <td>@item.BiddingAmount</td>
                    </tr>                    
                }    
                </tbody>
            </table>
        </section>
    </aside>

    <div class="clear"></div>

</section>

<div class="statistics">    
    <div class="statistics-title">
        <h2>Most popular hours            
            <span class="bids-date-paging">
                <span class="loading-icon-stats"></span>
                <span class="prev"><a href="#" title="Previous day"></a></span>
                <span class="current">@Model.MostlyBiddedHours.date.ToLongDateString()</span>
                <span><input style="display: none;" title="stat date" type="text" class="stat-datepicker" /></span>
                <span class="next"><a href="#" title="Next day"></a></span>                                                          
            </span>
        </h2>
    </div>
    
    <div class="statistics-graph">        
        <script src="@Url.Content("~/Scripts/highcharts.js")"></script>
        <script src="@Url.Content("~/Scripts/modules/exporting.js")"></script>               
        <script src="@Url.Content("~/Scripts/Statistics.js")" type="text/javascript"></script>
        <div class="page">
            <div id="container" style="min-width: 400px; height: 290px; margin: 0 auto; padding-top: 10px;"></div>
        </div>

        <table id="datatable" style=" display: none; ">
	        <thead>
		        <tr>
			        <th></th>
			        <th>Number of bids per time</th>
		        </tr>
	        </thead>
	        <tbody>
                @foreach (var item in Model.MostlyBiddedHours.timeStatistics)
                { 
		            <tr>
			            <th>@item.time</th>
			            <td>@item.numberOFBids</td>
		            </tr>                    
                }
	        </tbody>
        </table>
    </div>
</div>

<script type="text/javascript">

    function date_by_subtracting_days(date, days) {
        return new Date(
            date.getFullYear(), 
            date.getMonth(), 
            date.getDate() - days,
            date.getHours(),
            date.getMinutes(),
            date.getSeconds(),
            date.getMilliseconds()
        );
    }
    function date_by_adding_days(date, days) {
        return new Date(
            date.getFullYear(), 
            date.getMonth(), 
            date.getDate() + days,
            date.getHours(),
            date.getMinutes(),
            date.getSeconds(),
            date.getMilliseconds()
        );
    }
    $(function () {
        
        var today = new Date();
        var currentDate = new Date(@Model.currentDateResult.Year,@Model.currentDateResult.Month-1,@Model.currentDateResult.Day,@Model.currentDateResult.Hour,@Model.currentDateResult.Minute,@Model.currentDateResult.Second,@Model.currentDateResult.Millisecond);        
        var currentDateMax = new Date(@Model.currentDateResult.Year,@Model.currentDateResult.Month-1,@Model.currentDateResult.Day,@Model.currentDateResult.Hour,@Model.currentDateResult.Minute,@Model.currentDateResult.Second,@Model.currentDateResult.Millisecond);
        var canNext = false;

        $(".tabs-date-paging .prev a").click(function (event) {
            event.preventDefault();                                    
            $(".loading-icon").show();
            var ticks = date_by_subtracting_days(currentDate,1).getTime();            
            updateBidResults(ticks);

        });
        $(".tabs-date-paging .next a").live("click", function (event) {
            event.preventDefault();
            $(".loading-icon").show();
            var ticks = date_by_adding_days(currentDate,1).getTime();
            updateBidResults(ticks);
        });

        function updateBidResults(dateticks) {            
            $.ajax({
                type: 'POST',
                /*async: false,*/
                url: "/Dashboard/IndexAJAX",
                dataType: "json",
                data: { ticks: dateticks },
                success: function (data) {
                    if (data !== "") {                        
                        ctn = 1;
                        $("div.ui-tabs-panel").each(function () {
                            ctn2 = 0;
                            $("#tabs-bids-" + ctn + " table tbody tr td.group-name").each(function () {
                                $(this).find("p").text(data[ctn-1][ctn2].timeValue)
                                $(this).attr("class", "col-2 group-name " + data[ctn-1][ctn2].tempClass);                           
                                ctn2++;
                            });
                            ctn++;
                        });
                        //update dates and links
                        var m_names = new Array("January", "February", "March", 
                        "April", "May", "June", "July", "August", "September", 
                        "October", "November", "December");
                        var newdate = new Date(dateticks);                        
                        $(".tabs-date-paging .current").text(newdate.getDate() + " " + m_names[newdate.getMonth()] + " " + newdate.getFullYear());
                        currentDate = newdate;                        
                        //allow next (future) lookup
                        if (currentDate < currentDateMax) {      
                            if(canNext == false) {
                                var elem = document.createElement("span");
                                $(elem).attr("class", "next");
                                var anchor = document.createElement("a");
                                $(anchor).attr("href", "#");
                                $(anchor).attr("title", "Next day");
                                $(elem).append(anchor);
                                $(".tabs-date-paging").append(elem);
                                canNext = true;
                            }
                        }
                        else {
                            $(".tabs-date-paging .next").remove();
                            canNext = false;
                        }
                    }
                    else {
                        //console.log("Ups: empty data. Not updating");
                    }
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    $(".loading-icon").hide();
                    alert(xhr.status);
                    alert(thrownError);
                },
                complete: function (jqXHR, textStatus) {
                    $(".loading-icon").hide();
                }
            });
        }
    });

    $(function () {
        chartStatistics();
        var today = new Date();
        var currentDate = new Date(@Model.currentDateResult.Year,@Model.currentDateResult.Month-1,@Model.currentDateResult.Day,@Model.currentDateResult.Hour,@Model.currentDateResult.Minute,@Model.currentDateResult.Second,@Model.currentDateResult.Millisecond);        
        var currentDateMostPopular = new Date(@Model.MostlyBiddedHours.date.Year,@Model.MostlyBiddedHours.date.Month-1,@Model.MostlyBiddedHours.date.Day,@Model.MostlyBiddedHours.date.Hour,@Model.MostlyBiddedHours.date.Minute,@Model.MostlyBiddedHours.date.Second,@Model.MostlyBiddedHours.date.Millisecond);                
        var canNext = false;     
        
        function updateStatResults(dateticks) {            
            $.ajax({
                type: 'POST',
                /*async: false,*/
                url: "/Dashboard/ChartAJAX",
                dataType: "json",
                data: { ticks: dateticks },
                success: function (data) {
                    if (data !== "") {                        
                        ctn = 0;
                        $("#datatable tbody tr").each(function () {
                            $(this).find("th").text(data[ctn].time);
                            $(this).find("td").text(data[ctn].numberOFBids);                            
                            ctn++;
                        });
                        //update dates and links
                        var m_names = new Array("January", "February", "March", 
                        "April", "May", "June", "July", "August", "September", 
                        "October", "November", "December");
                        var newdate = new Date(dateticks);                        
                        $(".bids-date-paging .current").text(newdate.getDate() + " " + m_names[newdate.getMonth()] + " " + newdate.getFullYear());
                        currentDateMostPopular = newdate;            
                        var newdatestring = newdate.getMonth()+1 + "/" + newdate.getDate() + "/" + newdate.getFullYear()
                        $( ".stat-datepicker" ).datepicker("setDate",newdatestring);              
                        chartStatistics();
                    }
                    else {
                        //console.log("Ups: empty data. Not updating");
                    }
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    $(".loading-icon-stats").hide();
                    alert(xhr.status);
                    alert(thrownError);
                },
                complete: function (jqXHR, textStatus) {
                    $(".loading-icon-stats").hide();
                }
            });
        }
        
        /****************************************/   
        $(".bids-date-paging .prev a").click(function (event) {
            event.preventDefault();                                    
            $(".loading-icon-stats").show();
            var ticks = date_by_subtracting_days(currentDateMostPopular,1).getTime();            
            updateStatResults(ticks);

        });
        /****************************************/   
        $(".bids-date-paging .next a").live("click", function (event) {
            event.preventDefault();
            $(".loading-icon-stats").show();
            var ticks = date_by_adding_days(currentDateMostPopular,1).getTime();
            updateStatResults(ticks);
        });
        /****************************************/                  
        $( ".stat-datepicker" ).datepicker({
            dateFormat: 'mm/dd/yy',
            showOn: "button",
            //buttonImage: "../../Content/images/icons/datepicker-cal.png",
            buttonImage: "../../Content/images/icons/calendar.gif",
            buttonImageOnly: true,
            buttonText: "Stat date",
            onSelect: function( selectedDate ) {
                $(".loading-icon-stats").show();            
                var selectedDate = new Date(selectedDate);
                var ticks = selectedDate.getTime();
                updateStatResults(ticks);
            }
        }); 
    });
</script>
